<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class Account extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'create' => [
                'role_id' => 'required|integer',
                'account_name' => 'required|regex:/^[A-Za-z0-9_\-\x{4e00}-\x{9fa5}]+$/u|min:2',
                'account_email' => 'required|email',
                'account_phone' => 'required|regex:/^1[3456789]\d{9}$/',
                'account' => 'required|alpha_dash|min:2',
                'password' => 'required|between:6,32',
                'password_confirm' => 'required|same:password',
                'account_status' => 'required|in:1,2',
                'login_num' => 'integer',
                'login_ip' => 'ip',
                'login_time' => 'date'
            ],
            'update' => [
                'role_id' => 'required|integer',
                'account_name' => 'required|regex:/^[A-Za-z0-9_\-\x{4e00}-\x{9fa5}]+$/u|min:2',
                'account_email' => 'required|email',
                'account_phone' => 'required|regex:/^1[3456789]\d{9}$/',
                'account_status' => 'required|in:1,2',
            ],
            'modify' => [
                'password' => 'required|between:6,32',
                'password_confirm' => 'required|same:password',
            ],
        ];
    }

    public function messages(): array
    {
        return [
            'role_id.required' => '角色ID不能为空',
            'role_id.integer' => '角色ID必须是整数',

            'account_name.required' => '账号名不能为空',
            'account_name.regex' => '账号名只能由汉字、字母、数字和下划线_及破折号-组成',
            'account_name.min' => '账号名至少包含2个字符',

            'account_email.required' => '邮箱不能为空',
            'account_email.email' => '邮箱格式不正确',

            'account_phone.required' => '手机号不能为空',
            'account_phone.regex' => '手机号格式不正确',

            'account.required' => '账号不能为空',
            'account.alphaDash' => '账号只能由为字母和数字，下划线_及破折号-组成',
            'account.min' => '账号至少包含2个字符',

            'password.required' => '密码不能为空',
            'password.between' => '密码长度在6到32位字符之间',

            'password_confirm.required' => '确认密码不能为空',
            'password_confirm.same' => '两次密码输入不一致',

            'account_status.required' => '状态不能为空',
            'account_status.in' => '状态值只能是1或者2',

            'login_num' => '登陆次数必须是整数',

            'login_ip' => '登陆IP格式不对',

            'login_date' => '登陆时间格式不对',
        ];
    }
}
